package com.hera.base.web.vaadin.handler;

import com.hera.base.web.vaadin.component.FormAdvanced;
import com.hera.base.web.vaadin.component.Iconifiable;
import com.hera.base.web.vaadin.component.Selectable;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
import com.vaadin.ui.Component;
import com.vaadin.ui.DragAndDropWrapper.DragStartMode;
import com.vaadin.ui.DragAndDropWrapper.WrapperTargetDetails;
import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable;

public class ContainerDropHandler implements DropHandler{
	
	 /**
	 * 
	 */
	private static final long serialVersionUID = -5395649964041245578L;
	private AbsoluteLayout layout;
	private SelectableClickListener containerListener;
	
	public ContainerDropHandler(AbsoluteLayout layout) {
		this.layout=layout;		
	}
	
	public ContainerDropHandler(AbsoluteLayout layout, SelectableClickListener containerListener) {
		this.layout=layout;
		this.containerListener = containerListener;
	}

	@Override
	public AcceptCriterion getAcceptCriterion() {
	    // Accept all drops from anywhere
		 return AcceptAll.get();
	}
		
	@Override
	public void drop(DragAndDropEvent event) {
		WrapperTransferable transferable = (WrapperTransferable) event.getTransferable();
		WrapperTargetDetails details = (WrapperTargetDetails) event.getTargetDetails();
		//Component component = transferable.getDraggedComponent();
		Component component = transferable.getSourceComponent();
		//Viene de la paleta de diseño
		if(component instanceof Iconifiable){
			HeraDragAndDropWrapper componentWrapper =null;
			Selectable source = createComponent((Iconifiable) component);
			//Selectable source= containerListener.createComponent((Iconifiable) component);
			if(!(source instanceof FormAdvanced)){
				componentWrapper = new HeraDragAndDropWrapper(source);
				componentWrapper.setDragStartMode(DragStartMode.WRAPPER);
				componentWrapper.setSizeUndefined();
				
				layout.addComponent(componentWrapper,"left: "+(details.getMouseEvent().getClientX()-240)+"px; top: "+(details.getMouseEvent().getClientY()-100)+"px;");
				
				ComponentPosition pos = layout.getPosition(componentWrapper);
				
				//aca hacer save del componente.
				if(containerListener != null){
					source.addLayoutClickListener(containerListener);
					containerListener.seleccionarComponente(source);
					containerListener.crearComponente(source, pos.getTopValue(), pos.getLeftValue());
				}
			}else{
				if(containerListener != null){
					containerListener.agregarComponenteAvanzado(new Float(details.getMouseEvent().getClientY()-100), new Float(details.getMouseEvent().getClientX()-240));
				}
			}
			//se hace un drag and drop desde el formulario cargado de componentes
	     }else{
	    	 
	    	
			int xChange = details.getMouseEvent().getClientX()
							- transferable.getMouseDownEvent().getClientX();
			int yChange = details.getMouseEvent().getClientY()
					- transferable.getMouseDownEvent().getClientY();
			
			// Move the component in the absolute layout
			ComponentPosition pos = layout.getPosition(transferable.getSourceComponent());
			pos.setLeftValue(pos.getLeftValue() + xChange);
			pos.setTopValue(pos.getTopValue() + yChange);
			HeraDragAndDropWrapper componentbase = (HeraDragAndDropWrapper)transferable.getSourceComponent();
			System.out.println(componentbase.getComponentRoot());
			if(containerListener != null){
				containerListener.actualizarComponente((Selectable)componentbase.getComponentRoot(), pos.getTopValue(), pos.getLeftValue());
			}
	     }
	 
	}
	
	private Selectable createComponent(Iconifiable component) {
		return component.getSelectable();		
	}

	public SelectableClickListener getContainerListener() {
		return containerListener;
	}

	public void setContainerListener(SelectableClickListener containerListener) {
		this.containerListener = containerListener;
	}
	
}
